/*
 * @Author: Song Xinxin
 * @Date: 2022-10-27 23:02:35
 * @LastEditors: Song Xinxin
 * @LastEditTime: 2022-11-08 11:03:09
 * @FilePath: \WYW_0419_Oasis\JavaScripts\UI\UIGame.ts
 * @Description: 技能及血条UI
 */
import { DataCenterC, ModuleManager, UI } from "odin";
import { Consts } from "../Consts";
import { Data_PlayerAttr } from "../Modules/PlayerAttr/Data_PlayerAttr";
import { Data_Skill } from "../Modules/Skill/Data_Skill";
import { ModuleC_Skill } from "../Modules/Skill/Module_Skill";
import { ModuleC_Sound } from "../Modules/Sound/Module_Sound";
import { ModuleC_Weapon } from "../Modules/Weapon/Module_Weapon";
import { UIDeath } from "./UIDeath";
import { UILeaderboard } from "./UILeaderboard";
import { UI_GameUI } from "./UITemplate"

//技能及血条UI
export class UIGame extends UI_GameUI
{

    Player:GamePlay.Player;
    Character:GamePlay.Character;

    protected async onStart(): Promise<void> 
    {
        this.Player = await GamePlay.asyncGetCurrentPlayer();
        this.Character = this.Player.character;

        this.initButton();

        this.lockSkill();

        this.onChangeCountDown();

    }

    protected onShow(): void 
    {
        ModuleManager.instance.getModule(ModuleC_Sound).playGameBGM();

        this.onRefreshAttr();

    }

    /**
     * 游戏结束
     */
    public GameOver():void
    {
        this.Character.canMove = false;
		UI.instance.hidePanel(UIDeath);
        setTimeout(()=>
        {
            UI.instance.showPanel(UILeaderboard);
        },1000)
    }
    
    /**
     * 进入游戏时锁定技能按钮
     */
    public lockSkill():void
    {
        this.hide_shoot();
        this.hide_fly();
        this.hide_hide();
    }

    /**
     * 刷新玩家属性显示
     */
    public onRefreshAttr():void
    {
        let data = DataCenterC.instance.getModuleData(Data_PlayerAttr);
        data.onDataChange.add(()=>
        {
            this.progressBar_HP.setCurrentValue(data.getHP);
            this.text_HP.setText(`${data.getHP.toString()}/100`);
            this.text_kill.setText(`击杀 ${data.getkill.toString()}`);
            this.text_death.setText(`死亡 ${data.getdeath.toString()}`);
            this.text_count.setText(`收集 ${data.getcount.toString()}`);
        })
    }

    /**
     * 初始化按钮
     */
    public initButton():void
    {
        //跳跃
        this.button_jump.onClicked().add(()=>
        {
            this.Character.jump();
        })

        //按下射击摇杆开始射击
        this.rocker_fire.onJoyStickDown().add(()=>
        {
            ModuleManager.instance.getModule(ModuleC_Weapon).startshoot();
        });
        //松开摇杆停止射击
        this.rocker_fire.onJoyStickUp().add(()=>
        {
            ModuleManager.instance.getModule(ModuleC_Weapon).stopshoot();
        });
        
        //按下隐身技能按钮
        this.button_hide.onClicked().add(()=>
        {
            ModuleManager.instance.getModule(ModuleC_Skill).useHide();
        })

        //按下飞行技能按钮
        this.button_fly.onClicked().add(()=>
        {
            ModuleManager.instance.getModule(ModuleC_Skill).useFly();
        })
    }

    /**
     * 玩家死亡时锁定技能按钮
     */
    public Death():void
    {
        this.hide_jump();
        this.hide_shoot();
        this.hide_fly();
        this.hide_hide()
        UI.instance.showPanel(UIDeath);
    }

    /**
     * 玩家复活时解锁技能按钮
     */
    public Rebirth():void
    {
        //判断是否已解锁技能
        let data = DataCenterC.instance.getModuleData(Data_Skill);
        this.show_jump();
        if(data.ShootState == true)
            {
            this.show_shoot();
        }
        if(data.FlyState == true)
        {
            this.show_fly();
        }
        if(data.HideState == true)
        {
            this.show_hide();
        }
        UI.instance.hidePanel(UIDeath);
    }

    /**
     * 改变游戏倒计时显示
     * @param CountDown 游戏倒计时
     */
	public onChangeCountDown():void
	{
        Events.addServerListener("ChangeCountDown",(CountDown:number)=>
        {
            this.text_CountDown.setText(CountDown.toString());
        });
	}

    /**
     * 隐藏跳跃技能按钮
     */
    public hide_jump():void
    {
        //隐藏跳跃技能按钮
        this.button_jump.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.image_jump.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.text_jump.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
    }

    /**
     * 显示跳跃技能按钮
     */
    public show_jump():void
    {
        //显示跳跃技能按钮
        this.button_jump.setVisibility(MWGameUI.ESlateVisibility.Visible);
        this.image_jump.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
        this.text_jump.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
    }

    /**
     * 隐藏隐身技能按钮
     */
    public hide_hide():void
    {
        //隐藏隐身技能按钮
        this.button_hide.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.image_hide.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.text_hide.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
    }

    /**
     * 显示隐身技能按钮
     */
    public show_hide():void
    {
        //显示隐身技能按钮
        this.button_hide.setVisibility(MWGameUI.ESlateVisibility.Visible);
        this.image_hide.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
        this.text_hide.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
        this.text_hide.setText("隐身");
    }

    /**
     * 隐身技能CD
     */
    public hideCD():void
    {
        let CD = Consts.SkillAttr.Hide.CD;
        let interval = setInterval(()=>
        {
            CD--;
            this.text_hide.setText(CD.toString());
            if(CD <= 0)
            {
                this.text_hide.setText("隐身");
                clearInterval(interval);
            }
        },1000)
    }

    /**
     * 隐藏飞行技能按钮
     */
    public hide_fly():void
    {
        //隐藏飞行技能按钮
        this.button_fly.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.image_fly.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.text_fly.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
    }

    /**
     * 显示飞行技能按钮
     */
    public show_fly():void
    {
        //显示飞行技能按钮
        this.button_fly.setVisibility(MWGameUI.ESlateVisibility.Visible);
        this.image_fly.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
        this.text_fly.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
        this.text_fly.setText("飞行");
    }

    /**
     * 飞行技能CD
     */
    public flyCD():void
    {
        let CD = Consts.SkillAttr.Fly.CD;
        let interval = setInterval(()=>
        {
            CD--;
            this.text_fly.setText(CD.toString());
            if(CD <= 0)
            {
                this.text_fly.setText("飞行");
                clearInterval(interval);
            }
        },1000)
    }

    /**
     * 隐藏射击摇杆
     */
    public hide_shoot():void
    {
        ModuleManager.instance.getModule(ModuleC_Weapon).stopshoot();
        //隐藏射击摇杆
        this.rocker_fire.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
        this.text_fire.setVisibility(MWGameUI.ESlateVisibility.Collapsed);
    }

    /**
     * 显示射击摇杆
     */
    public show_shoot():void
    {
        //显示射击摇杆
        this.rocker_fire.setVisibility(MWGameUI.ESlateVisibility.Visible);
        this.text_fire.setVisibility(MWGameUI.ESlateVisibility.SelfHitTestInvisible);
    }

}